﻿using System.Text;

namespace Koala.Pro.Common
{
    public class DatabaseInfo
    {
        private string server;

        private string database;

        private string userID;

        private string password;

        public string Server
        {
            get
            {
                return server;
            }
            set
            {
                server = value;
            }
        }

        public string Database
        {
            get
            {
                return database;
            }
            set
            {
                database = value;
            }
        }

        public string UserId
        {
            get
            {
                return userID;
            }
            set
            {
                userID = value;
            }
        }

        public string Password
        {
            get
            {
                return password;
            }
            set
            {
                password = value;
            }
        }

        public string EncryptConnectionString => EncodeBase64(ConnectionString);

        public string ConnectionString
        {
            get
            {
                string text = "";
                if (!string.IsNullOrEmpty(UserId) && !string.IsNullOrEmpty(Password))
                {
                    return $"Persist Security Info=False;Data Source={server};Initial Catalog={database};User ID={userID};Password={password};Packet Size=4096;Pooling=true;Max Pool Size=100;Min Pool Size=1";
                }
                return $"Persist Security Info=False;Data Source={server};Initial Catalog={database};Integrated Security=SSPI;Packet Size=4096;Pooling=true;Max Pool Size=100;Min Pool Size=1";
            }
        }

        public string OleDbConnectionString => "Driver={SQL Server};" + ConnectionString;

        public DatabaseInfo()
        {
        }

        public DatabaseInfo(string connectionString)
        {
            server = GetSubItemValue(connectionString, "服务名称");
            if (server == null)
            {
                server = GetSubItemValue(connectionString, "Data Source");
            }
            if (server == null)
            {
                server = GetSubItemValue(connectionString, "server");
            }
            database = GetSubItemValue(connectionString, "数据库名称");
            if (database == null)
            {
                database = GetSubItemValue(connectionString, "Initial Catalog");
            }
            if (database == null)
            {
                database = GetSubItemValue(connectionString, "database");
            }
            userID = GetSubItemValue(connectionString, "用户名称");
            if (userID == null)
            {
                userID = GetSubItemValue(connectionString, "User ID");
            }
            if (userID == null)
            {
                userID = GetSubItemValue(connectionString, "uid");
            }
            password = GetSubItemValue(connectionString, "用户密码");
            if (password == null)
            {
                password = GetSubItemValue(connectionString, "Password");
            }
            if (password == null)
            {
                password = GetSubItemValue(connectionString, "pwd");
            }
        }

        private string GetSubItemValue(string itemValueString, string subKeyName)
        {
            string[] array = itemValueString.Split(new char[1] { ';' });
            for (int i = 0; i < array.Length; i++)
            {
                if (array[i].ToLower().IndexOf(subKeyName.ToLower()) >= 0)
                {
                    int num = array[i].IndexOf("=");
                    return array[i].Substring(num + 1);
                }
            }
            return null;
        }

        private string EncodeBase64(string source)
        {
            byte[] bytes = Encoding.UTF8.GetBytes(source);
            return Convert.ToBase64String(bytes, 0, bytes.Length);
        }
    }

}
